<?php 
/**
* @author Oliver Leisalu
* @package DomAr
* @subpackage Search
*/
class DomArSearchIndex {
	
	public function clearIndex( DomArObject $object, $property = NULL ) {
		
		
		if( $object ) {
			if( $object->id == 0)
				return true;
			
			$where = '';
		}
		
		
		
		DomSql::query('DELETE FROM '.$this->getDbTable().' WHERE item = '.(int)$object->id.' AND property = \''.addslashes($property).'\'');
	}
		
	public function createIndex( DomArObject $object, $property = NULL ) {
		$keys = analyzeText( $this->text );
		foreach( $keys as $key => $t ) {
			$item = new ItemIndex();
			$item->value = $key;
			$item->property = $property;
			$item->item = $this->id;
			$item->create();
			$item->active = $this->active;		
		}
	}
	
	public function updateIndex( DomArObject $object, $property = NULL ) {
		
	}
	
	public function createWholeIndex() {
		DomSql::query('TRUNCATE TABLE '.$this->getDbTable().'');
		
		
		
	}
	
	public function dataQuery() {
		
	}
	
	public function query() {
		
	}
	
	public function analyzeText( $text ) {
		$words = explode(' ',trim($text));
		$list = array();
		foreach( $words as $word ) {
			if( strlen($word) > 2 ) {
				$list[strtolower($word)]++;
			}
		}
		return $list;
	}
	

	public function getTable() {
		
	}
	
	
}

/**
* @author Oliver Leisalu
* @package DomAr
* @subpackage Search
*/
class DomArProperty_SearchIndex extends DomArProperty implements 
														IDomArProperty, 
														IDomArPropertyValidator,
														IDomArPropertyAlterTableListener,
														IDomArPropertyInsertListener,
														IDomArPropertyUpdateListener,
														IDomArPropertyInitListener,
														IDomArPropertyLoadListener  {

	protected $fields;
	
	public function __construct( $name, array $fields = array()  ) {
		parent::__construct( $name );
		$this->fields = $fields;
	}
	
	public function init( DomArObject $object, array &$values ) {
		return NULL;
	}
	
	public function load( DomArObject $object, &$propertyValue ) {
		return new DomArSearchIndex();
	}
	
	public function isLoaded( DomArObject $object, &$propertyValue ) {
		return is_object($propertyValue);
	}
	
	public function validate( DomArObject $object, &$propertyValue, array &$customParams = array() ) {
		return spl_object_hash($propertyValue) == spl_object_hash($object->{$this->name}) ? 0 : 1;
	}
	
	public function insert( DomArObject $object, &$propertyValue, DomArValueSet $set  ) {
		
	}
	
	public function update( DomArObject $object, &$propertyValue, DomArValueSet $set ) {
		
	}
	
	public function alterTable( DomArTable $table ) {
		
	}
	
	
	
	
	public function updateIndex() {
		
	}
}
?>